package com.pmcc.core.web;

import com.pmcc.core.config.security.JwtUser;
import com.pmcc.core.entity.security.Role;
import com.pmcc.core.service.RoleService;
import com.pmcc.utils.AntPage;
import com.pmcc.utils.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by syk on 17/7/11.
 */
@RestController()
@RequestMapping("api/v1/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @GetMapping("queryByPage")
    public ResultBean queryByPage(AntPage antPage) {
        JwtUser jwtUser = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return roleService.queryByPage(antPage, jwtUser);
    }

    /**
     * 新增角色
     *
     * @param role
     * @return
     */
    @PostMapping("saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody Role role) {
        return roleService.saveOrUpdate(role);
    }

    @DeleteMapping("")
    public ResultBean delete(@RequestBody Role model) {
        return roleService.delete(model);
    }


    @GetMapping("/queryRoleMenu")
    public ResultBean queryRoleMenu(String roleId) {
        return roleService.queryRoleMenu(roleId);
    }

    @PostMapping("/{roleId}/saveRoleMenus")
    @ResponseBody
    public ResultBean saveRoleMenus(@PathVariable("roleId") String roleId, @RequestBody List<String> menus) {
        return roleService.saveRoleMenus(roleId, menus);
    }

    @GetMapping("queryList")
    public ResultBean queryList() {
        ResultBean resultBean = new ResultBean();
        resultBean.setRows(roleService.queryList());
        resultBean.setResultCode(ResultBean.SUCCESS);
        return resultBean;
    }
}
